package com.pro.common.service.user.service;

import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.pro.common.api.auth.IUserService;
import com.pro.common.api.enums.EnumSysRole;
import com.pro.common.api.exception.BusinessException;
import com.pro.common.api.model.ILoginInfo;
import com.pro.common.service.properties.CommonProperties;
import com.pro.common.service.user.dao.UserBankCardDao;
import com.pro.common.api.user.model.db.User;
import com.pro.common.service.user.model.db.UserBankCard;
import com.pro.framework.api.database.IContext;
import com.pro.framework.api.util.AssertUtil;
import com.pro.framework.api.util.LambdaUtil;
import com.pro.framework.api.util.PasswordUtils;
import com.pro.framework.api.util.inner.FunSerializable;
import com.pro.framework.mybatisplus.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 代付渠道 服务实现类
 */
@Service
public class UserBankCardService extends BaseService<UserBankCardDao, UserBankCard> {
    @Autowired
     private CommonProperties commonProperties;
//    @Autowired
     private IUserService userService;
    @Autowired
    public void setUserService(List<IUserService> userServices) {
        this.userService = userServices.stream().filter(s -> s instanceof BaseService<?, ?>).findFirst().orElseGet(() -> userServices.get(0));
    }

    public List<UserBankCard> getActiveList() {
        return this.lambdaQuery().orderByAsc(UserBankCard::getSort).list();
    }

//    /**
//     * 更新属性
//     *
//     * @return
//     */
//    public boolean updates(UserBankCard param, UserBankCard setData, FunSerializable<UserBankCard, ?>... functions) {
//        UpdateChainWrapper<UserBankCard> wrapper = this.update();
//        wrapper.setEntity(param);
////        LambdaUpdateChainWrapper<UserBankCard> wrapper = this.lambdaUpdate().setEntity(param);
//        for (FunSerializable<UserBankCard, ?> function : functions) {
//            wrapper.set(LambdaUtil.resolveCache(function).getPropName(), function.apply(setData));
//        }
//       return wrapper.update();
//    }

//    @Override
//    public UserBankCard getById(Long id) {
//        return super.getById(id);
//    }

    @Override
    public boolean save(IContext context, UserBankCard entity) {
        this.saveOrUpdateBeforeCheck(context,entity);
        return super.save(context, entity);
    }


    @Override
    public boolean updateById(IContext context, UserBankCard entity) {
        this.saveOrUpdateBeforeCheck(context, entity);
        return super.updateById(context, entity);
    }

    @Override
    public boolean saveOrUpdate(IContext context, UserBankCard entity) {
        this.saveOrUpdateBeforeCheck(context, entity);
        return super.saveOrUpdate(entity);
    }

    private void saveOrUpdateBeforeCheck(IContext context, UserBankCard entity) {
        User user = null;
        EnumSysRole sysRole = context instanceof ILoginInfo ? ((ILoginInfo) context).getSysRole() : EnumSysRole.USER;
        switch (sysRole){
            case USER:
                user = (User) userService.getById(entity.getUserId());
                AssertUtil.isTrue(user.getTkPassword().equals(PasswordUtils.encrypt_tkPassword(entity.getTkPassword())), "提款密码错误");
                break;
            case ANONYMOUS:
                throw new BusinessException(401, "请先登录");
        }
        if (entity.getId() == null) {
            if (user == null) {
                user = (User) userService.getById(entity.getUserId());
            }
            entity.setUsername(user.getUsername());
            entity.setIsDemo(user.getIsDemo());
        }
    }
}

